<?php
class ArticlesController extends AppController {

	var $name = 'Articles';
	var $helpers = array('Html', 'Form');
    
    
    function index() 
    {
        $this->set('choose1',5); 
        $this->Article->recursive = -1;
        $data = $this->Article->find('all',array('conditions'=>array('status'=>1)));
        
        $this->set('data',$data);
    }    

    function index_admin()  
    {
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout='ajax';
        }
        else
        {
            $this->Session->setFlash('You are forbidden for this page');
            $this->redirect(array('controller'=>'jobs', 'action'=>'login_admin'),null, true);
        }             
    }
    
    function filter_admin()  
    {
        $this->layout='ajax';
        if(!empty($this->data['status']))
        {
            $this->Testimonial->recursive=0;
                                                                 
            $query = "SELECT * FROM `articles`";                                                     
                                                                 
            $mydate = date("Y-m-d", mktime(0, 0, 0, date('m')-1, date('d'), date('Y')));
            if($this->data['status']==2)
            { $query.= " WHERE status = 1"; }
            if($this->data['status']==3)
            { $query.= " WHERE status = 1 AND created < '".$mydate."'"; }
            if($this->data['status']==4)
            { $query.= " WHERE status = 0"; }
            if($this->data['status']==5)
            { $query.= " WHERE status = 0 AND created < '".$mydate."'"; }
            
            $query.= " ORDER BY created DESC";
            
            $articles = $this->Article->query($query);
            $this->set('articles',$articles);
        }
        
        if(empty($this->data))
        {
            $query = "SELECT * FROM `articles` ORDER BY created DESC";
            $articles = $this->Article->query($query);
            $this->set('articles',$articles);
        }
        
    }
    

    function add_admin()
    {
        if(!empty($this->data))
        {
            $this->Article->save($this->data);
            $this->redirect(array('controller'=>'articles','action'=>'filter_admin'));    
        }
        $this->layout = 'ajax';
    }

    function edit_admin( $id = null )
    {
        if(!empty($this->data))
        {
            $this->Article->save($this->data);
            $this->redirect(array('controller'=>'articles','action'=>'filter_admin'));    
        }
        else
        {
            $this->layout = 'ajax';
            $this->Article->recursive=-1;
            $this->data = $this->Article->read(null,$id);
        }
    }    

    function approve_admin( $id = null )
    { 
        $this->data['Article']['id']=$id;
        $this->data['Article']['status']=1;
        //$this->data['Article']['created']=date("Y-m-d");
        $this->Article->save($this->data);
        $this->redirect(array('controller'=>'articles','action'=>'filter_admin'));
    }

    function cancel_admin( $id = null )
    { 
        $this->data['Article']['id']=$id;
        $this->data['Article']['status']=0;
        $this->Article->save($this->data);
        $this->redirect(array('controller'=>'articles','action'=>'filter_admin'));
    }
    
    function delete_admin($id)
    {  
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            $this->Article->del($id);
            $this->redirect(array('controller'=>'articles','action'=>'filter_admin'));
        }        
    }    
}
?>